'use strict';

function IsObject(val) {
  return Object.prototype.toString.call(val) === '[object Object]';
}

// var Spinner = require('cli-spinner').Spinner;
 
// var spinner = new Spinner('processing.. %s');
// spinner.setSpinnerString('|/-\\');
// spinner.start();

function SpinnerFn(text = 'loading..') {
  var Spinner = require('cli-spinner').Spinner;
  var spinner = new Spinner( text + ' %s');
  spinner.setSpinnerString('|/-\\');
  spinner.start()
  return spinner
}

async function Sleep(time = 1000) {
  await new Promise((resolve, reject) => {
    setTimeout(resolve, time);
  })
}

function Spawn(command, args, options) {
  const win32 = process.platform === 'win32';
  const cmd = win32 ?  'cmd' : command;
  const cmdArgs = win32 ? ['/c'].concat(command, args) : args;
  return require('child_process').spawn(cmd, cmdArgs, options || {})
}

function SpawnAsync(command, args, options) {
  return new Promise((resolve, reject) => {
    const p = Spawn(command, args, options);
    p.on('error', e => {
      reject(e)
    })
    p.on('exit', c => {
      resolve(c)
    })
  })
}


module.exports = {
  IsObject,
  SpinnerFn,
  Sleep,
  Spawn,
  SpawnAsync
};
